<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="d" uri="http://claudiushauptmann.com/jsp-decorator/"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<d:decorate filename='../shared/user-masterpage.jsp'>
    <d:content placeholder='title'>Afeka Bid System</d:content>
    <d:content placeholder='body'>
        <c:set var="now" value="<%=new java.util.Date()%>" />
        <table class="table-sales">
            <tr>
                <th>Product</th>
                <th>Type</th>
                <th>Date Started</th>
                <th>Date End</th>
                <th>Status</th>
                <c:if test="${isAdmin}">
                <th>Action</th>
                </c:if>
            </tr>
        <c:forEach items="${homePageModel.getProducts()}" var="product">
            <tr>
                <td>
                    <a href="product/${product.id}">${product.name}</a>
                </td>
                <td>
                    <c:choose>
                        <c:when test="${product.type == 1}">
                            Tender
                        </c:when>
                        <c:otherwise>
                            Auction
                        </c:otherwise>
                    </c:choose>
                </td>
                <td>
                    ${product.dateCreated}
                </td>
                <td>
                    ${product.dateEnd}
                </td>
                <td>
                    <c:choose>
                        <c:when test="${product.status == 1}">
                            closed
                        </c:when>
                        <c:when test="${product.dateEnd != null && product.dateEnd lt now}">
                            ended
                        </c:when>
                        <c:otherwise>
                            opened
                        </c:otherwise>
                    </c:choose>
                </td>
                <c:if test="${isAdmin}">
                <td>
                    <c:if test="${product.status == 0}">
                    <a class="end-button" data-prod="${product.id}">close</a>
                    </c:if>
                </td>
                </c:if>
            </tr>
        </c:forEach>
        </table>

        <c:if test="${isAdmin}">
            <script type="text/javascript">
                $(document).ready(function(){
                    $(".end-button").click(function(e){
                        var id = $(this).data("prod");
                        $.post("/admin/closeAuction", {productId : id}, function(r){
                            alert(r.string);
                            location.reload();
                        });
                        return false;
                    });
                });
            </script>
        </c:if>
    </d:content>
</d:decorate>